package com.wanghai.carwashing.common.util;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 输入校验的工具类
 * @author 王海
 */
public class ValidationUtil {
    public static final String REGEX_DATE = "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$";
    public static final String REGEX_CELLPHONE = "^((13[0-9])|(14[5,7])|(15[^4,\\D])|(18[^4,\\D]))\\d{8}$";
    public static final String REGEX_EMAIL = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    public static final String REGEX_QQ = "^[1-9]*[1-9][0-9]*$";
    public static final String REGEX_TIME_INTERVAL = "^([0-1][0-9]|[2][0-3]):([0-5][0-9])-([0-1][0-9]|[2][0-3]):([0-5][0-9])$";
    public static final String REGEX_ALPHABET_NUMBER_UNDERLINE_CHINESE = "^[a-zA-Z0-9_\u4e00-\u9fa5]*$";
    public static final String REGEX_ALPHABET_CHINESE = "^[a-zA-Z\u4e00-\u9fa5]*$";
    public static final String REGEX_ALPHABET_NUMBER_CHINESE = "^[a-zA-Z0-9\u4e00-\u9fa5]*$";
    public static final String REGEX_ALPHABET_NUMBER_CHINESE_BRACKETS = "^[a-zA-Z（）0-9\u4e00-\u9fa5]*$";
    public static final String REGEX_ADDRESS = "^[a-zA-Z（），0-9\u4e00-\u9fa5]*$";
    public static final String REGEX_ALPHABET_NUMBER = "^[a-zA-Z0-9]*$";
    public static final String REGEX_NUMBER_DOT = "^[.0-9]*$";;
    public static final String REGEX_NUMBER = "^[0-9]*$";
    public static final String REGEX_TELEPHONE = "\\d{3}-\\d{8}|\\d{4}-\\d{7}";
    public static final String REGEX_ALPHABET = "^[a-zA-Z]*$";
    public static final String REGEX_URL= "(https?)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]";
    public static final int MAX_LENGTH_SUMMARY = 250;
    public static final int MAX_LENGTH_TITLE = 100;
    public static final int MAX_LENGTH_NAME = 50;

    // i18n chars
    public static final String MESSAGE_EMPTY_TITLE = "请填写标题";
    public static final String MESSAGE_MAX_LENGTH_TITLE = "标题长度不能大于" +
            ValidationUtil.MAX_LENGTH_TITLE + "个字符";
    public static final String MESSAGE_EMPTY_SUMMARY = "请填写简介概要";
    public static final String MESSAGE_MAX_LENGTH_SUMMARY = "标题长度不能大于" +
            ValidationUtil.MAX_LENGTH_SUMMARY + "个字符";

    private ValidationUtil() {
    }

    public static boolean notNull(Object object) {
        return object != null;
    }

    public static boolean isNull(Object object) {
        return object == null;
    }

    public static boolean After(Date a, Date b) {
        return a.after(b);
    }

    /**
     * 在max和min之间，包括max和min <br>
     * [min, max]
     */
    public static boolean between(String value, int min, int max) {
        if (value == null || min >= max)
            return false;
        int len = value.length();
        return (len >= min && len <= max);
    }

    /**
     * 在max和min之间，包括max和min <br>
     * [min, max]
     */
    public static boolean between(Integer value, int min, int max) {
        if (value == null || min >= max)
            return false;
        return (value >= min && value <= max);
    }

    /**
     * 检查是否是email格式
     */
    public static boolean email(String email) {
        if (email == null || !email.contains("@"))
            return false;
        Pattern pattern = Pattern.compile(REGEX_EMAIL);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

    /**
     * 检查是否是qq格式
     */
    public static boolean qq(String qq) {
        if (qq == null)
            return false;
        Pattern pattern = Pattern.compile(REGEX_QQ);
        Matcher matcher = pattern.matcher(qq);
        return matcher.matches();
    }

    /**
     * 检查是否是cellphone格式
     */
    public static boolean cellphone(String cellphone) {
        if (cellphone == null)
            return false;
        Pattern pattern = Pattern.compile(REGEX_CELLPHONE);
        Matcher matcher = pattern.matcher(cellphone);
        return matcher.matches();
    }

    public static boolean matches(String value, String regex) {
        if (value == null || regex == null)
            return false;
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(value);
        return matcher.matches();
    }

    public static boolean isAlphabetOrNumberOrUnderlineOrChinese(String value) {
        if (value == null)
            return false;
        Pattern pattern = Pattern.compile(REGEX_ALPHABET_NUMBER_UNDERLINE_CHINESE);
        Matcher matcher = pattern.matcher(value);
        return matcher.matches();
    }

    public static boolean isAlphabetOrNumber(String value) {
        if (value == null)
            return false;
        Pattern pattern = Pattern.compile(REGEX_ALPHABET_NUMBER);
        Matcher matcher = pattern.matcher(value);
        return matcher.matches();
    }

    public static boolean isAlphabet(String value) {
        if (value == null)
            return false;
        Pattern pattern = Pattern.compile(REGEX_ALPHABET);
        Matcher matcher = pattern.matcher(value);
        return matcher.matches();
    }

    public static boolean isNumber(String value) {
        if (value == null)
            return false;
        char[] chars = value.toCharArray();
        for (char c : chars) {
            if (!Character.isDigit(c))
                return false;
        }
        return true;
    }

    public static boolean notNumber(String value) {
        return !isNumber(value);
    }

    public static boolean equals(int expected, int value) {
        return expected == value;
    }

    public static boolean notEquals(int expected, int value) {
        return expected != value;
    }

    public static boolean empty(String value) {
        if (value == null)
            return true;
        if (value.length() <= 0)
            return true;
        return false;
    }

    public static boolean notEquals(String value1, String value2) {
        if (value1 == null || value2 == null)
            return false;
        return !value1.equals(value2);
    }

    public static boolean isNotNull(Object value) {
        return !isNull(value);
    }

    public static boolean notEquals(Long v1, Long v2) {
        if (v1 == null || v2 == null)
            return false;
        return !v1.equals(v2);
    }

    public static boolean equals(Long v1, Long v2) {
        return !notEquals(v1, v2);
    }
}
